package tgc.edu.exam.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.entity.BookType;
import tgc.edu.exam.entity.Books;
import tgc.edu.exam.form.BookForm;
import tgc.edu.exam.service.BookService;
import tgc.edu.exam.service.BookTypeService;

@Controller
@RequestMapping("/book")
public class BooksController {

	@Autowired
	private BookService bookService;
	@Autowired
	private BookTypeService bookTypeService;
	@RequestMapping("/manage")
	public String manage() {
		return "book/manage";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public HashMap<String, Object> page(DataGridParam param,BookForm form) {
		Pageable pageable = param.getPageable();
		Specification<Books> spec = buildSpec(form);
		Page<Books> page = bookService.findAll(spec,pageable);
		return DataGridUtils.buildResult(page);
		
	}

	private Specification<Books> buildSpec(BookForm form) {
		Specification<Books> spec = new Specification<Books>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<Books> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (StringUtils.hasText(form.getTitle())) {
					rules.add(cb.like(root.get("title"), "%"+form.getTitle()+"%"));
				}else if(StringUtils.hasText(form.getByid())) {
					rules.add(cb.like(root.get("byid"), "%"+form.getByid()+"%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	
	@RequestMapping("/edit")
	public String edit(Books book,ModelMap map) {
		Books model = new Books();
		if(book.getId()!=null) {
			model = bookService.findById(book.getId());
		}
		List<BookType> types = bookTypeService.findAll();
		map.put("types", types);
		map.put("model", model);
		return "book/edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpServletRequest request, Books book,@RequestParam( value="files",required=false)MultipartFile multipartFile) throws IllegalStateException, IOException {
		try {
			if(book.getId()!=null) {
				book = bookService.findById(book.getId());
				bookService.savebook(multipartFile,book);
			}else {
				bookService.savebook(multipartFile,book);
			}
			return "保存成功";
		}catch(Exception e) {
			e.printStackTrace();
			return "编号可能重复";
		}
		
	} 
	 
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		bookService.download(id, request, response);
		return null;
	}
 
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		bookService.deleteById(id);
		return "删除成功！";
	}
}
